package com.liugh.controller.zhongche;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.annotation.CurrentUser;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardDownload;
import com.liugh.enums.zhongche.DownloadEnum;
import com.liugh.model.DownloadModel;
import com.liugh.service.queue.RedisSend;
import com.liugh.service.zhongche.IStandardDownloadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**标准申请下载 .
 * Created by Fatao on 2018/11/2 .
 */

@RestController
@RequestMapping("/standard_download")
@Api(description = "标准申请下载模块")
public class StandardDownloadController extends BaseRestController {


    @Autowired
    private IStandardDownloadService standardDownloadService;

    @Autowired
    RedisSend redisSend;


    /**
     * 删除一条下载 .
     * @param id .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("删除申请下载")
    @DeleteMapping("/delete")
    public ResponseModel<String> deleteStandardDownload(@RequestParam("id") int id) throws BusinessException {
        if (standardDownloadService.getStandardDownById(id).getStatus() == DownloadEnum.APPLYING_DOWNLOAD.getCode()) {
            if (standardDownloadService.delStandDownload(id)) {
                return fillSuccessData(PublicResultConstant.SUCCEED);
            } else {
                throw new BusinessException(PublicResultConstant.ERROR);
            }
        } else {
            return fillFailData(PublicResultConstant.ERROR_APPLY_STATUS);
        }

    }


    /**
     * 分页获取所有的申请下载 .
     * @param pageNum .
     * @param pageSize .
     * @return .
     */

    @ApiOperation("获取所有的申请下载条目")
    @GetMapping("/get_all")
    public ResponseModel<Page<StandardDownload>> getAllStandardDownload(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize) {

        return fillSuccessDataPage(standardDownloadService.getAllStandardDownload(new Page<>(pageNum, pageSize)));
    }


    /**
     * 获取我申请的下载条目 .
     * @param pageNum .
     * @param pageSize .
     * @param user .
     * @return .
     */
    @ApiOperation("获取我申请所有的申请下载条目")
    @GetMapping("/get_my")
    public ResponseModel<Page<StandardDownload>> getAllStandardDownloadByUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
                                                                              @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize,
                                                                              @CurrentUser User user) {

        return fillSuccessDataPage(standardDownloadService.getAllStandardDownloadByUser(new Page<>(pageNum, pageSize), user));
    }


    @PostMapping("/add")
    @ApiOperation("添加申请")
    public ResponseModel<StandardDownload> addStandDownload(@RequestBody StandardDownload download,
                                                           @CurrentUser User user) throws BusinessException {
        download = standardDownloadService.addStandDownload(download,user);
        if (download == null) {
            throw new BusinessException(PublicResultConstant.APPLY_FILE);
        }
        return fillSuccessData(download);
    }


    @PostMapping("/agree")
    @ApiOperation("同意申请")
    public ResponseModel<StandardDownload> agreeDownloadApply(@RequestBody StandardDownload download,
                                                              @CurrentUser User user) throws BusinessException {
        if (download.getStatus() != DownloadEnum.APPLYING_DOWNLOAD.getCode()) {
            throw new BusinessException(PublicResultConstant.ERROR_APPLY_STATUS);
        } else {
            if (!redisSend.sendDownloadMsg(JSONObject.toJSONString(download))) {
                throw new BusinessException(PublicResultConstant.ERROR);
            }
        }

        download.setStatus(DownloadEnum.AGREE_DOWNLOAD.getCode());
        download.setApproveTime(new Date());
        download.setApproveNo(user.getUserNo());
        download = standardDownloadService.updateDownload(download);

        if (download == null) {
            throw new BusinessException(PublicResultConstant.DATA_ERROR);
        }
        return fillSuccessData(download);
    }



    @ApiOperation("驳回申请")
    @PostMapping("/refuse")
    public ResponseModel<StandardDownload> refuseDownLoad(@RequestBody StandardDownload download) throws BusinessException {
        if (download.getStatus() != DownloadEnum.APPLYING_DOWNLOAD.getCode()) {
            throw new BusinessException(PublicResultConstant.ERROR_APPLY_STATUS);
        }
        download.setStatus(DownloadEnum.DISAGREE_DOWNLOAD.getCode());
        download = standardDownloadService.refuseDownloadApply(download);
        if (download == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(download);
    }

    @ApiOperation("获取文件的下载链接及原始文件名")
    @ApiImplicitParam(name = "id",value = "standDown的ID",required = true,paramType = "query",dataType = "int")
    @GetMapping("/download")
    public ResponseModel<DownloadModel> getPathOriginName(@RequestParam(value = "id",required = true)int id) throws BusinessException {
        DownloadModel downloadModel = standardDownloadService.download(id);
        if (downloadModel == null) {
            throw new BusinessException(PublicResultConstant.FILE_NOT_EXIST);
        }
        return fillSuccessData(downloadModel);
    }

}
